package com.sfx.算法专题.位运算;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: sfx
 * Date: 2023-08-21
 * Time: 8:51
 */
public class Code_1_hammingWeight {

    static class Solution1 {
        // you need to treat n as an unsigned value
        public int hammingWeight(int n) {
            int ans = 0;
            while (n != 0) {
                n &= (n - 1);
                ans++;
            }
            return ans;
        }
    }

    static class Solution2 {
        // you need to treat n as an unsigned value
        public int hammingWeight(int n) {
            int ans = 0;
            for (int i = 0; i < 32; ++i) {
                if ((n & 1) == 1) ans++;
                n >>= 1;
            }
            return ans;
        }
    }
}